package com.finance.cooperate.strategy.strategy;

import com.finance.cooperate.common.constant.Constant;
import com.finance.cooperate.common.utils.StringUtil;
import com.finance.cooperate.dao.entity.secondary.StrategyInfo;
import com.finance.cooperate.feature.core.feature.FeatureDefinition;
import com.finance.cooperate.strategy.core.strategy.AbstractStrategyHandler;
import com.finance.cooperate.strategy.core.strategy.StrategyScope;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName FraudStrategy
 * @Description 用户反欺诈策略
 * @Author shenanxin
 * @Date 2023/8/23 16:05
 * @Modify ...
 */
@Component
@Slf4j
public class FraudStrategy extends AbstractStrategyHandler {

    @Autowired
    private StrategyScope strategyScope;


    @Override
    public Long execute(StrategyInfo strategyInfo) {

        try {

            // 最低命中阈值
            int repeatCount = 2;
            String extra1 = strategyInfo.getExtra1();
            if (!StringUtil.isEmptyOrNvl(extra1)) {
                repeatCount = Integer.parseInt(extra1);
            }

            Map<String, String> featureMap = strategyScope.getFeatureMap();

            int appCount = -1;
            String appCountStr = featureMap.getOrDefault(FeatureDefinition.f_app_install_detail_same_app_count.getValue(), String.valueOf(Constant.MISSING_VALUE));
            if (!StringUtil.isEmptyOrNvl(appCountStr)) {
                appCount = Integer.parseInt(appCountStr);
            }

            if (appCount >= repeatCount) { // 表示策略命中

                Map<String, Object> data = new HashMap<>();
                data.put(FeatureDefinition.f_app_install_detail_same_app_count.getValue(), appCount);
                data.put(FeatureDefinition.f_app_install_detail_same_uid.getValue(), featureMap.getOrDefault(FeatureDefinition.f_app_install_detail_same_uid.getValue(), String.valueOf(Constant.MISSING_VALUE)));

                super.hitResult(data, strategyInfo);

                return strategyInfo.getId();
            }


        } catch (Exception e) {
            log.error("反欺诈策略执行错误：", e);
        }

        return null;
    }

    @Override
    public String setStrategyName() {
        return "fraud_strategy";
    }
}
